import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hong_vpn_new_20240626/ui/location_list/location_list.dart';
import 'package:hong_vpn_new_20240626/utils/Configs/text_style_config.dart';
import 'package:hong_vpn_new_20240626/utils/global.dart';
import 'package:hong_vpn_new_20240626/utils/models/vpn_list.dart';
import 'package:hong_vpn_new_20240626/utils/utils.dart';
import 'package:hong_vpn_new_20240626/utils/zz_navigator.dart';

class LocationBtnWidget extends StatefulWidget {
  const LocationBtnWidget({super.key});

  @override
  State<LocationBtnWidget> createState() => _LocationBtnWidgetState();
}

class _LocationBtnWidgetState extends State<LocationBtnWidget> {
  StreamSubscription? stream;
  @override
  void initState() {
    super.initState();
    stream = eventBus.on<VpnListModel>().listen((event) {
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    stream?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => ZZNavigator.push(widget: const LocationListWidget()),
      child: SizedBox(
        height: 60,
        child: Global.currVpn == null
            ? Center(
                child: Text(
                "Please select a location",
                style: TextStyleConfig.middle,
              ))
            : Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text("Nation:", style: TextStyleConfig.value),
                  const SizedBox(width: 8),
                  Text(Global.currVpn!.hotName, style: TextStyleConfig.value),
                  const SizedBox(width: 8),
                  const Text("|"),
                  const SizedBox(width: 8),
                  Image.asset("assets/images/美国国旗 1.png")
                ],
              ),
      ),
    );
  }
}
